package org.kjtc.service;

import org.kjtc.commom.CommonUtil;
import org.kjtc.commom.XmlSerializer;
import org.kjtc.entity.UserInfo;
import org.kjtc.entity.WeChatConfig;
import org.kjtc.entity.WeChatInfo;
import org.kjtc.mapper.LogMapper;
import org.kjtc.mapper.TimerMapper;
import org.kjtc.web.StatisticalWorkTask;
import org.kjtc.web.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Administrator on 2017/12/6 0006.
 */
@Component
public class TimerService {

    @Resource(name="weChatInfo")
    private WeChatInfo weChatInfo;

    @Autowired
    private TimerMapper taskMapper;

    @Autowired
    private LogService logService;

    //时间间隔(一天)
    private static final long PERIOD_DAY = 24 * 60 * 60 * 1000;

    @Scheduled(cron = "0 0 6 ? * *")
    public void timer(){
        System.out.println("["+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"]:   插入Sprint数据定时任务启动成功(每日)");
        int num=taskMapper.insertSprintDataEveryDay();
        if(num <= 0){
            throw new RuntimeException("["+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"]:   插入Sprint数据失败(每日)");
        }else{
            System.out.println("["+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"]:   插入Sprint数据成功(每日)");
        }
        System.out.println("["+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"]:   插入Sprint数据定时任务执行结束(每日)");
    }

    public void insertBackLogDataEverybody(){
        File file = new File(CommonUtil.WECHAT_CONFIG_PATH);
        if (file.exists()) {
            XmlSerializer<WeChatConfig> xSerializer = new XmlSerializer<WeChatConfig>();
            try {
                weChatInfo.setStatisticsTime(xSerializer.unMarshallXml(file, WeChatConfig.class).getStatisticsTime());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        Calendar calendar = Calendar.getInstance();
        String statisticsTime = weChatInfo.getStatisticsTime();
        calendar.set(Calendar.HOUR_OF_DAY, Integer.valueOf(statisticsTime.substring(0,statisticsTime.indexOf(":")))); //时
        calendar.set(Calendar.MINUTE, Integer.valueOf(statisticsTime.substring(statisticsTime.indexOf(":")+1,statisticsTime.length())));//分
        calendar.set(Calendar.SECOND, 0);
        Date date=calendar.getTime(); //第一次执行定时任务的时间
        //如果第一次执行定时任务的时间 小于当前的时间
        //此时要在 第一次执行定时任务的时间加一天，以便此任务在下个时间点执行。如果不加一天，任务会立即执行。
        if (date.before(new Date())) {
            date = this.addDay(date, 1);
        }
        Timer timer = new Timer();
        StatisticalWorkTask task = new StatisticalWorkTask(taskMapper,logService);
        //安排指定的任务在指定的时间开始进行重复的固定延迟执行。
        timer.schedule(task,date,PERIOD_DAY);
    }

    public void sendWorkDataEveryDay(){

        File file = new File(CommonUtil.WECHAT_CONFIG_PATH);
        if (file.exists()) {
            XmlSerializer<WeChatConfig> xSerializer = new XmlSerializer<WeChatConfig>();
            try {
                weChatInfo.setAccess(xSerializer.unMarshallXml(file, WeChatConfig.class).getAccess());
                weChatInfo.setPushTime(xSerializer.unMarshallXml(file, WeChatConfig.class).getPushTime());
                weChatInfo.setPushType(xSerializer.unMarshallXml(file, WeChatConfig.class).getPushType());
                weChatInfo.setPushUrl(xSerializer.unMarshallXml(file, WeChatConfig.class).getPushUrl());
                weChatInfo.setWeChatModuleID(xSerializer.unMarshallXml(file, WeChatConfig.class).getWeChatModuleID());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        Calendar calendar = Calendar.getInstance();
        String pushTime = weChatInfo.getPushTime();
        calendar.set(Calendar.HOUR_OF_DAY, Integer.valueOf(pushTime.substring(0,pushTime.indexOf(":")))); //时
        calendar.set(Calendar.MINUTE, Integer.valueOf(pushTime.substring(pushTime.indexOf(":")+1,pushTime.length())));//分
        calendar.set(Calendar.SECOND, 0);
        Date date=calendar.getTime(); //第一次执行定时任务的时间
        //如果第一次执行定时任务的时间 小于当前的时间
        //此时要在 第一次执行定时任务的时间加一天，以便此任务在下个时间点执行。如果不加一天，任务会立即执行。
        if (date.before(new Date())) {
            date = this.addDay(date, 1);
        }
        Timer timer = new Timer();
        Task task = new Task(taskMapper,logService);
        //安排指定的任务在指定的时间开始进行重复的固定延迟执行。
        timer.schedule(task,date,PERIOD_DAY);
    }
    // 增加或减少天数
    public Date addDay(Date date, int num) {
        Calendar startDT = Calendar.getInstance();
        startDT.setTime(date);
        startDT.add(Calendar.DAY_OF_MONTH, num);
        return startDT.getTime();
    }
}
